//
//  PLDatePicker.m
//
//  Created by KimmanYip on 2018/12/21.
//

#import "PLDatePicker.h"
#import "PLColor.h"
#import "UIView+PLExtension.h"
#import "Masonry.h"
#import "DateTools.h"

static const CGFloat kViewHeight = 273;

typedef void(^DPCompletionBlock)(NSString *dateString);

@interface PLDatePicker ()

@property (nonatomic, strong) UIView        *contentView;
@property (nonatomic, strong) UIDatePicker  *picker;

@property (nonatomic, copy) DPCompletionBlock completion;
@property (nonatomic, copy) NSString *formatter;
@property (nonatomic, strong) NSDate *minDate;
@property (nonatomic, strong) NSDate *maxDate;
@property (nonatomic, assign) UIDatePickerMode mode;
@property (nonatomic, strong) NSDate *defaultDate;      // 默认选中的时间

@end

@implementation PLDatePicker

- (instancetype)initWithCompletion:(void(^)(NSString *dateString))completion
{
    return [self initWithFormatter:@"yyyy-MM-dd" completion:completion];
}

- (instancetype)initWithMinDate:(NSDate *)minDate maxDate:(NSDate *)maxDate completion:(void(^)(NSString *dateString))completion {
    self = [super init];
    if (self) {
        [self initData];
        self.completion = completion;
        self.minDate = minDate;
        self.maxDate = maxDate;
        [self buildUI];
    }
    return self;
}

- (instancetype)initWithFormatter:(NSString *)formatter completion:(void(^)(NSString *dateString))completion {
    self = [super init];
    if (self) {
        [self initData];
        self.formatter = formatter;
        self.completion = completion;
        [self buildUI];
    }
    return self;
}

- (instancetype)initWithFormatter:(NSString *)formatter mode:(UIDatePickerMode)mode defaultDate:(NSDate *)defaultDate completion:(void(^)(NSString *dateString))completion {
    self = [super init];
    if (self) {
        [self initData];
        self.formatter = formatter;
        self.completion = completion;
        self.mode = mode;
        self.defaultDate = defaultDate;
        [self buildUI];
    }
    return self;
}

- (void)initData
{
    self.formatter = @"yyyy-MM-dd";
    self.mode = UIDatePickerModeDate;
}

- (void)show {
    dispatch_async(dispatch_get_main_queue(), ^{
        UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
        [keyWindow endEditing:YES];
        [keyWindow addSubview:self];
        [self showAnimation];
    });
}

- (void)showAnimation {
    self.contentView.y = self.frame.size.height;
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.y = self.frame.size.height-kViewHeight;
    }];
}

- (void)dismiss {
    [UIView animateWithDuration:0.3 animations:^{
        self.contentView.y = self.frame.size.height;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)buildUI
{
    self.frame = [UIScreen mainScreen].bounds;
    
    // 背景半透明
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
    
    self.contentView = [UIView new];
    [self.contentView setBackgroundColor:[UIColor whiteColor]];
    [self addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(kViewHeight);
        make.left.right.and.bottom.equalTo(self);
    }];
    
    
    // 顶部
    UIView *topView = [UIView new];
    topView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(45);
        make.left.top.and.right.equalTo(self.contentView);
    }];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"选择日期";
    titleLabel.textColor = ColorWithHex(0x303030);
    titleLabel.font = [UIFont systemFontOfSize:18];
    [topView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(topView);
    }];
    
    UIView *lineH = [UIView new];
    [lineH setBackgroundColor:ColorWithHex(0xEFF1F0)];
    lineH.backgroundColor = [UIColor lightGrayColor];
    [topView addSubview:lineH];
    [lineH mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.bottom.equalTo(topView);
        make.height.mas_equalTo(0.5);
    }];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:ColorWithHex(0xcccccc) forState:UIControlStateNormal];
    [cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [cancelBtn addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:cancelBtn];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.and.bottom.equalTo(topView);
        make.width.mas_equalTo(55);
    }];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setTitle:@"完成" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:ColorWithHex(0xBA8833) forState:UIControlStateNormal];
    [confirmBtn.titleLabel setFont:[UIFont systemFontOfSize:18]];
    [confirmBtn addTarget:self action:@selector(confirmButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [topView addSubview:confirmBtn];
    
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.and.bottom.equalTo(topView);
        make.width.mas_equalTo(55);
    }];
    
    //  选择器
    self.picker = [UIDatePicker new];
    self.picker.datePickerMode = self.mode;
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];//设置为中文
    if (self.defaultDate) {
        [self.picker setDate:self.defaultDate];
    }
    self.picker.locale = locale;
    [self.contentView addSubview:self.picker];
    [self.picker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.contentView);
        make.top.equalTo(topView.mas_bottom);
        make.bottom.equalTo(self);
    }];
    if (self.minDate) {
        [self.picker setMinimumDate:self.minDate];
    }
    if (self.maxDate) {
        [self.picker setMaximumDate:self.maxDate];
    }
}


#pragma mark - Action
- (void)cancelButtonAction
{
    [self dismiss];
}

- (void)confirmButtonAction
{
    if (self.completion) {
        self.completion([self.picker.date formattedDateWithFormat:self.formatter]);
    }
    [self dismiss];
}

@end
